import scrapy


class DoubanSpider(scrapy.Spider):
    name = 'douban'
    allowed_domains = ['douban.com', 'doubanio.com']
    start_urls = ['https://movie.douban.com/top250?start=0&filter=']
    page_num = 1

    def parse(self, response):
        print("---->>>>", response.status, response.url)

        # 提取li标签对象
        li_list = response.xpath("//ol[@class='grid_view']/li")
        for li_temp in li_list:
            img_src = li_temp.xpath(".//img/@src").extract_first()
            movie_title = li_temp.xpath(".//div[@class='hd']//a//span[1]/text()").extract_first()
            rating_num = li_temp.xpath(".//div[@class='star']//span[@class='rating_num']/text()").extract_first()
            people_num = li_temp.xpath(".//div[@class='star']//span[4]/text()").extract_first()

            yield {
                "type": "info",
                "movie_title": movie_title,
                "img_src": img_src,
                "rating_num": rating_num,
                "people_num": people_num
            }

            # 生成Request，爬取图片
            yield scrapy.Request(url=img_src, callback=self.parse_img, cb_kwargs={"img_name": movie_title})

        # 构造下一页的URL(?start=25&filter=)
        self.page_num += 1
        if self.page_num <= 10:
            # print(self.page_num)
            next_page_url = "https://movie.douban.com/top250?start=%d&filter=" % ((self.page_num - 1) * 25)
            # 生成Request，爬取下一页的数据
            yield scrapy.Request(url=next_page_url, callback=self.parse)

    def parse_img(self, response, img_name):
        print("----图片url----", response.url)
        yield {
            'type': "img",
            'img_name': img_name + ".png",
            'img_bytes': response.body
        }
